//
//  PinterestMainViewController.m
//  TestDemo
//
//  Created by zhangwenqiao on 2018/12/17.
//  Copyright © 2018年 zhangwenqiao. All rights reserved.
//

#import "PinterestMainViewController.h"
#import "PaintingCollectionViewCell.h"
#import "TypeCollectionViewCell.h"
#import "BannerReusableView.h"
#import "TypeDetailViewController.h"
#import "PaintingDetailViewController.h"
#import "PinterestCollectionReusableView.h"
#import "PinterestTypeReusableView.h"
#import <MJRefresh/MJRefresh.h>

@interface PinterestMainViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, WSLWaterFlowLayoutDelegate>
{
    WSLWaterFlowLayout * _flow;
    NSMutableArray *bannerArray;
    
    NSMutableArray * _array;
    NSInteger count;
    NSInteger dataNumber;
}
@end

@implementation PinterestMainViewController

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _flow = [[WSLWaterFlowLayout alloc] init];
        _flow.delegate = self;
        _flow.flowLayoutStyle = self.flowLayoutStyle;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, navViewHeight, Screen_Width, Screen_Height - navViewHeight - 0) collectionViewLayout:_flow];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self.collectionView registerNib:[UINib nibWithNibName:@"PaintingCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PaintingCollectionViewCell"];
        [self.collectionView registerNib:[UINib nibWithNibName:@"TypeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"TypeCollectionViewCell"];
        if (navViewHeight > 64) {
            if (@available(iOS 11.0, *)) {
                _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            }else {
                
            }
        }
        [self.collectionView registerNib:[UINib nibWithNibName:@"BannerReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BannerReusableView"];
        [self.collectionView registerClass:[PinterestCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PinterestCollectionReusableView"];
        [self.collectionView registerClass:[PinterestTypeReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PinterestTypeReusableView"];
    }
    return _collectionView;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"异形瀑布 头部多层嵌套 无扩展性";
    _array = [NSMutableArray array];
    for (int i = 0 ; i < 2 * 30; i++) {
//        [_array addObject:@(arc4random() % 200)];
        [_array addObject:@((Screen_Width - 30) / 2 + 10 + 20 + 10 + 20 + 10 + (arc4random() % (50 - 0) + 0))];
    }
    NSArray *array = @[@"http://www.wohoney.com/images/case_img_74.png", @"http://www.wohoney.com/images/case_img_72.png", @"http://www.wohoney.com/images/case_img_71.png", @"http://www.wohoney.com/images/case_img_82.png"];
    bannerArray = [NSMutableArray arrayWithArray:array];
    dataNumber = 10;
    
    [self.view addSubview:self.collectionView];
    [self collectionViewUI];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)collectionViewUI {
//    [self.collectionView registerNib:[UINib nibWithNibName:@"" bundle:nil] forCellWithReuseIdentifier:@""];
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (void)loadMoreData {
    NSLog(@"dataNumber:%ld -- _array.count:%ld", dataNumber, _array.count - 10);
    if (dataNumber == (_array.count - 10)) {
        [self.collectionView.mj_footer endRefreshing];
        return;
    }
    dataNumber = dataNumber + 10;
    NSLog(@"模拟刷新数据量--%ld", dataNumber);
//    [self.collectionView reloadData];
    [self.collectionView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self->dataNumber == 40) {
            [self.collectionView.mj_footer endRefreshing];
        }
    });
    if (dataNumber) {
        [self.collectionView.mj_footer endRefreshing];
    }
}

#pragma mark - Help Methods
////返回样式4所需的宽高信息
//- (NSArray *)proportionsForItem{
//    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"WSLWaterFlowHorizontalGrid" ofType:@"plist"];
//    NSArray *layoutInfo = [[NSArray alloc] initWithContentsOfFile:plistPath];
//    return layoutInfo;
//}

#pragma mark - WSLWaterFlowLayoutDelegate
//返回每个item大小
- (CGSize)waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(waterFlowLayout.flowLayoutStyle == (WSLWaterFlowLayoutStyle)0){
        if (indexPath.section == 1) {
            return CGSizeMake(0, [_array[indexPath.section * 10 + indexPath.row] floatValue]);
        }else if (indexPath.section == 0) {
            return CGSizeZero;
        }else {
            return CGSizeZero;
        }
    }else if (waterFlowLayout.flowLayoutStyle == (WSLWaterFlowLayoutStyle)1){
        return CGSizeMake([_array[indexPath.section * 10 + indexPath.row] floatValue], 0);
    }else if (waterFlowLayout.flowLayoutStyle == (WSLWaterFlowLayoutStyle)2){
        return CGSizeMake([_array[indexPath.section * 10 + indexPath.row] floatValue], 100);
    }else if (waterFlowLayout.flowLayoutStyle == (WSLWaterFlowLayoutStyle)3){
//        //每一个最小的正方形单元格的边长
//        CGFloat  average = (self.view.frame.size.width - [self edgeInsetInWaterFlowLayout:waterFlowLayout].left * 2 - 3 * [self columnMarginInWaterFlowLayout:waterFlowLayout])/4.0;
//        NSDictionary * proportion = [self proportionsForItem][indexPath.row];
//        CGSize itemSize =  CGSizeMake(average * [proportion[@"width"] intValue]+ ([proportion[@"width"] intValue] == 2 ? [self columnMarginInWaterFlowLayout:waterFlowLayout] : 0), average * [proportion[@"height"] intValue] + ([proportion[@"height"] intValue] == 2 ? [self rowMarginInWaterFlowLayout:waterFlowLayout] : 0));
        return CGSizeZero;
    }else{
        return CGSizeMake(0, 0);
    }
}

/** 头视图Size */
-(CGSize )waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForHeaderViewInSection:(NSInteger)section{
    if (section == 0) {
        if (bannerArray.count % 4 == 0) {
            count = bannerArray.count / 4;
        }else {
            count = bannerArray.count / 4 + 1;
        }
        return CGSizeMake(Screen_Width, Screen_Width / 728 * 426 + (Screen_Width - 50) / 4 * count + 20 + 10 * (count - 1));
    }else {
        return CGSizeZero;
    }
}

/** 脚视图Size */
-(CGSize )waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForFooterViewInSection:(NSInteger)section{
    return CGSizeZero;
}

/** 列数*/
-(CGFloat)columnCountInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout{
    return 2;
}
/** 行数*/
//-(CGFloat)rowCountInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout{
//    return 0;
//}
/** 列间距*/
-(CGFloat)columnMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout{
    return 5;
}
/** 行间距*/
-(CGFloat)rowMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout{
    return 5;
}
/** 边缘之间的间距*/
-(UIEdgeInsets)edgeInsetInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout{
    if (waterFlowLayout.flowLayoutStyle == (WSLWaterFlowLayoutStyle)3){
        return UIEdgeInsetsMake(20, 20, 20, 20);
    }
    return UIEdgeInsetsMake(0, 10, 0, 10);
}

#pragma mark --CollectionDelegate--
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        if (bannerArray.count > 0) {
            return 1;
        }else {
            return 0;
        }
    }else if (section == 1) {
        return dataNumber;
    }else {
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        TypeCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"TypeCollectionViewCell" forIndexPath:indexPath];
        if (!cell) {
            cell = (TypeCollectionViewCell *)[[NSBundle mainBundle] loadNibNamed:@"TypeCollectionViewCell" owner:nil options:nil];
            //            cell = [[TypeCollectionViewCell alloc] init];
        }
//        cell.dataSourceArray = bannerArray;
        return cell;
    }else {
        PaintingCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"PaintingCollectionViewCell" forIndexPath:indexPath];
        if (!cell) {
            cell = (PaintingCollectionViewCell *)[[NSBundle mainBundle] loadNibNamed:@"PaintingCollectionViewCell" owner:nil options:nil];
        }
        if (indexPath.row % 2 != 0) {
            cell.imageView.image = [UIImage imageNamed:@"two.jpg"];
            cell.nameLabel.text = [NSString stringWithFormat:@"小僵尸-%ld", indexPath.row];
        }else {
            cell.imageView.image = [UIImage imageNamed:@"touxiang.jpg"];
            cell.nameLabel.text = [NSString stringWithFormat:@"小女孩-%ld", indexPath.row];
        }
        return cell;
    }
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.section == 0) {
//        return CGSizeMake(Screen_Width, (Screen_Width - 30) / 2.5 / 2 * 1 + 20);
//    }else {
//        return CGSizeMake((Screen_Width - 30) / 2, (Screen_Width - 30) / 2 + 10 + 20 + 10 + 20 + 10);
//    }
//}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        TypeDetailViewController *type = [TypeDetailViewController initTypeDetailViewController:@"分类啊"];
        [self.navigationController pushViewController:type animated:YES];
    }
    if (indexPath.section == 1) {
        PaintingDetailViewController *painting = [[PaintingDetailViewController alloc] init];
        if (indexPath.row % 2 != 0) {
            painting.titleName = @"小僵尸";
            painting.image = [UIImage imageNamed:@"two.jpg"];
        }else {
            painting.titleName = @"小女孩";
            painting.image = [UIImage imageNamed:@"touxiang.jpg"];
        }
        [self.navigationController pushViewController:painting animated:YES];
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        if (indexPath.section == 0) {
            PinterestCollectionReusableView *bannerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PinterestCollectionReusableView" forIndexPath:indexPath];
            if (!bannerView) {
                bannerView = [[PinterestCollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Width / 728 * 426 + (Screen_Width - 50) / 4 * count + 20 + 10 * (count - 1))];
            }
            bannerView.banerArray = bannerArray;
            bannerView.dataSourceArray = bannerArray;
            bannerView.count = count;
            return bannerView;
        }else {
//            UICollectionReusableView *view = [[UICollectionReusableView alloc] init];
//            return view;
            PinterestTypeReusableView *typeView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PinterestTypeReusableView" forIndexPath:indexPath];
            if (!typeView) {
                typeView = [[PinterestTypeReusableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 0)];
            }
            return typeView;
        }
//        if (indexPath.section == 0 && bannerArray.count > 0) {

//        }else {
//            return nil;
//        }
    }else {
        BannerReusableView *bannerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BannerReusableView" forIndexPath:indexPath];
        if (!bannerView) {
            bannerView = [[BannerReusableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 0)];
        }
        bannerView.banerArray = bannerArray;
        return bannerView;
    }
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    if (section == 0) {
//        if (bannerArray.count > 0)  {
//            return CGSizeMake(Screen_Width, Screen_Width / 728 * 426);
//        }
//    }
//    return CGSizeZero;
//}

#pragma mark --CollectionLayout--
//定义整个CollectionViewCell与整个View的间距
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return UIEdgeInsetsMake(0, 10, 10, 10);//（上、左、下、右）
//}


@end
